<template>
  <div class="sub-filter" v-if='filterLoading === false && filterData'>
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a @click='changeBrand(item.id)' :class='{ active: item.id === filterData.selectedBrand }' href="javascript:;" v-for="item in filterData.brands" :key="item.id">{{ item.name }}</a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}</div>
      <div class="body">
        <a @click='changeProp(item, subItem.id)' :class='{ active: subItem.id === item.selectedAttr }' href="javascript:;" v-for="subItem in item.properties" :key="subItem.id">{{ subItem.name }}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
  </div>
</template>
<script>
import { reqSubCategoryFilter } from '@/api/category'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    // 监听二级类目ID 获取数据
    const filterData = ref({})
    const filterLoading = ref(false)
    const route = useRoute()
    watch(() => route.params.id, (newVal) => {
      // 有值且处在二级类名路由下
      if (newVal && route.path === `/category/sub/${newVal}`) {
        filterLoading.value = true
        reqSubCategoryFilter(route.params.id).then(data => {
          // 数据加上全部这个选项
          // 还要加上一个选中的ID标识
          data.result.brands.unshift({ id: null, name: '全部' })
          data.result.selectedBrand = null
          data.result.saleProperties.forEach(item => {
            item.properties.unshift({ id: null, name: '全部' })
            item.selectedAttr = null
          })
          // console.log(data.result)
          filterData.value = data.result
          filterLoading.value = false
        })
      }
    }, { immediate: true })
    // 获取参数，传递给父组件使用
    const getFilterParams = () => {
      const filterParams = {}
      const attrs = []
      filterParams.brandId = filterData.value.selectedBrand
      filterData.value.saleProperties.forEach(p => {
        const attr = p.properties.find(attr => attr.id === p.selectedAttr)
        if (attr && attr.id !== undefined) {
          attrs.push({ groupName: p.name, propertyName: attr.name })
        }
      })
      if (attrs.length) filterParams.attrs = attrs
      return filterParams
    }
    // 1 记录当前选择品牌id
    const changeBrand = (brandId) => {
      if (filterData.value.selectedBrand === brandId) return
      filterData.value.selectedBrand = brandId
      emit('filter-change', getFilterParams())
    }
    // 2 记录选择销售属性
    const changeProp = (item, attrId) => {
      if (item.selectedAttr === attrId) return
      item.selectedAttr = attrId
      emit('filter-change', getFilterParams())
    }
    return { filterData, filterLoading, changeBrand, changeProp }
  }
}
</script>
<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
    .xtx-skeleton {
      padding: 10px 0;
    }
  }
</style>
